package pe.com.bbva.listeners;


import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.PropertyConfigurator;
import org.springframework.stereotype.Repository;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import pe.com.bbva.mantenimiento.bo.ParametroBO;
import pe.com.bbva.mantenimiento.domain.Parametro;
import pe.com.bbva.util.Constantes;


/**
 * Application Lifecycle Listener implementation class LogConfiguratorListener
 *
 */

@Repository
public class LogConfiguratorListener implements ServletContextListener {
	
	private static WebApplicationContext springContext;
	
	private ParametroBO parametroBO = null;

	public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }

    public void contextInitialized(ServletContextEvent arg0) {
    	
    	//System.out.println("----- Inicializando Contexto targps ----");
    	try {
					
    	Properties prop = new Properties();
        	
    	springContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
        String strRutalog=Constantes.RUTALOGDEFAULT;
        parametroBO=(ParametroBO)springContext.getBean("parametroBO");        
		try {
			Parametro prutalog= parametroBO.findByNombreParametro(Constantes.RUTALOG);
			strRutalog=(prutalog.getValor()==null?"":prutalog.getValor());
		} catch (Exception e) {
			strRutalog=Constantes.RUTALOGDEFAULT;
		}
    	
    	    	     
        //System.out.println("Path:"+ strRutalog);
        
    	PropertyConfigurator.configure(prop);
    	
		
    	prop.setProperty("log4j.rootLogger", "info,logfile"); 
    	
    	/** Inicio LogConsola 
    	 * Para consola local - comentar para el pase a produccion
    	 **/ 
    	
//    	prop.setProperty("log4j.rootLogger", "error, stdout"); 
//    	prop.setProperty("log4j.appender.stdout", "org.apache.log4j.ConsoleAppender");
//    	prop.setProperty("log4j.appender.stdout.layout", "org.apache.log4j.PatternLayout");
//    	prop.setProperty("log4j.appender.stdout.layout.ConversionPattern", "%d [%c] %x (%F:%L) - %m%n");
//    	
//    	prop.setProperty("log4j.appender.dest1.Threshold", "DEBUG");
//    	
//    	prop.setProperty("log4j.logger.org.hibernate", "debug");
//    	prop.setProperty("log4j.logger.org.springframework", "debug");
//    	prop.setProperty("log4j.logger.org.hibernate.hql.ast.AST", "error");
//    	prop.setProperty("log4j.logger.org.hibernate.SQL", "trace");
//    	prop.setProperty("log4j.logger.org.hibernate.type", "trace");
//    	prop.setProperty("log4j.logger.org.hibernate.tool.hbm2ddl", "warn");
//    	prop.setProperty("log4j.logger.org.hibernate.hql", "debug");
//    	prop.setProperty("log4j.logger.org.hibernate.cache", "error");
//    	prop.setProperty("log4j.logger.org.hibernate.jdbc", "debug");
    	
//    	prop.setProperty("log4j.logger.com.opensymphony.xwork2.util.OgnlUtil", "error");
//    	prop.setProperty("log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack", "error");

    	
    	/**Fin LogConsola**/
    	
    	prop.setProperty("log4j.appender.logfile", "org.apache.log4j.RollingFileAppender");
    	prop.setProperty("log4j.appender.logfile.file", strRutalog);    	
    	//add
    	prop.setProperty("log4j.appender.logfile.ImmediateFlush","true");
    	prop.setProperty("log4j.appender.logfile.Append","true");
    	prop.setProperty("log4j.appender.logfile.MaxFileSize", "5MB");
    	prop.setProperty("log4j.appender.logfile.MaxBackupIndex", "3");   	
    	
    	prop.setProperty("log4j.appender.logfile.layout", "org.apache.log4j.PatternLayout");
    	prop.setProperty("log4j.appender.logfile.layout.ConversionPattern", "%d %5p [%t] (%F:%L) - %m%n");    	
    	
    	prop.setProperty("log4j.logger.org.hibernate", "debug");
    	prop.setProperty("log4j.logger.org.springframework", "debug");
    	prop.setProperty("log4j.logger.org.hibernate.hql.ast.AST", "error");
    	prop.setProperty("log4j.logger.org.hibernate.SQL", "trace");
    	prop.setProperty("log4j.logger.org.hibernate.type", "trace");
    	prop.setProperty("log4j.logger.org.hibernate.tool.hbm2ddl", "warn");
    	prop.setProperty("log4j.logger.org.hibernate.hql", "debug");
    	prop.setProperty("log4j.logger.org.hibernate.cache", "error");
    	prop.setProperty("log4j.logger.org.hibernate.jdbc", "debug");
    	
    	prop.setProperty("log4j.logger.com.opensymphony.xwork2.util.OgnlUtil", "error");
    	prop.setProperty("log4j.logger.com.opensymphony.xwork2.ognl.OgnlValueStack", "error");
    	prop.setProperty("log4j.logger.com.opensymphony.xwork2.util.logging.commons.CommonsLogger","info");
    	
    	prop.setProperty("log4j.logger.pe.com.bbva", "info");
		
		PropertyConfigurator.configure(prop);
		
		//System.out.println("Log configurated!");
    	} catch (Exception e) {
			System.out.println("Error configurando Log4j targps:"+e.getMessage());
		}
		
		
    }
	
}
